package com.smzd.framework.core.actions;

import java.util.Map;

import org.springframework.validation.BindingResult;

import com.smzd.core.domain.IOwnedEntity;
import com.smzd.core.domain.IntIdEntity;
import com.smzd.framework.utils.db.ORPage;

public abstract class AbstractOwnerController<E extends IOwnedEntity<O>, O extends IntIdEntity> extends
        AbstractController<E> {

    protected abstract O getOwner();

    /**
     * 默认的实现。必须用户登录，返回请求的数据列表。<br>
     * 提供Ajax的Json数据返回格式。
     * 
     * @throws Exception
     */
    public String getListData(Map<String, Object> model, ORPage<O, E> page, BindingResult result) throws Exception {
        if (page.getOwner() == null || page.getOwner().getId() == null) {
            page.setOwner(getOwner());
            System.out.println("--自动为获取列表数据装载了owner：" + page.getOwner());
        }
        System.out.println("--自动为获取列表数据装载了owner：" + page.getOwner());
        return super.getListData(model, page, result);
    }

    @Override
    protected String postAddData(E elem) throws Exception {
        if (elem.getOwner() == null || elem.getOwner().getId() == null) {
            elem.setOwner(getOwner());
        }
        return super.postAddData(elem);
    }

    public String toStaticListPage(Map<String, Object> model, ORPage<O, E> page, BindingResult result) {
        if (page.getOwner() == null || page.getOwner().getId() == null) {
            page.setOwner(getOwner());
            System.out.println("--自动为获取列表数据装载了owner：" + page.getOwner());
        }
        return super.toStaticListPage(model, page, result);
    }

}
